const mysql = require("mysql2")
const { userDataBase, originDataBase } = require("../config/mysqlConfig")

const user = mysql.createPool({
    port: '3306',
    host: originDataBase.HOST,
    user: originDataBase.USER,
    password: originDataBase.PASSWORD,
    database: originDataBase.DATABASE
})

exports.query = function (sql, values) {
    return new Promise((resolve, reject) => {
        user.getConnection(function (err, connection) {
        if (err) {
          reject(err)
          //console.log(err, "数据库连接失败");
          resolve({
            code: 500,
          })
        } else {
          //console.log("数据库连接成功");
          connection.query(sql, values, (err, results) => {
            if (err) {
              reject(err)
              resolve({
                code: 400
              })
            } else {
   
              resolve({
                code: 200,
                results,
              })
              connection.release()
              //resolve(rows)
            }
            //connection.release() // 释放连接池
          })
        }
      })
    })
  }
  
